package com.aixin.project.servlet;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.data.AxSession;
import com.aixin.project.data.CommonConstants;
import com.aixin.project.data.Course;
import com.aixin.project.data.Expert;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.util.RequestUtil;

public class ExpertServlet extends BaseServlet {

	@Override
	protected Template execute(HttpServletRequest request,
			HttpServletResponse response, Context ctx) {
		
		AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
		if(axsession==null){
		  ctx.put("uid", "-1");
		}
		else{
		  ctx.put("uid", axsession.getUid());
		}
		
		int page = 1;
		long expertid = RequestUtil.getLong(request, "id", -1);
	
		List<Expert> expertList = ExpertHandler.expertList(page);
		List<Course> courseList=new ArrayList<Course>();
		Expert expert=new Expert();
		
		
		int maxpage = ExpertHandler.getMaxpage();

        if (expertid == -1) {
            if (expertList != null && expertList.size() > 0) {
                expert = expertList.get(0);
                courseList = ExpertHandler.expertComingCourses(expert.getId());
            }
        } else {
            expert = ExpertHandler.find(expertid);
            if(null != expert)
                courseList = ExpertHandler.expertComingCourses(expert.getId());
        }
		
		ctx.put("maxpage", maxpage);
		ctx.put("page", page);
		ctx.put("expertList", expertList);
		ctx.put("expertInfo", expert);
		ctx.put("courseList", courseList);

        request.setAttribute("navexpert",true);
		return getTemplate("expert.vm");
	}

}
